package com.gsj.web.controller.user;

import com.github.pagehelper.PageInfo;
import com.gsj.domain.role.Role;
import com.gsj.domain.system.Dept;
import com.gsj.domain.user.User;
import com.gsj.service.role.RoleService;
import com.gsj.service.system.DeptService;
import com.gsj.service.user.UserService;
import com.gsj.web.basecrotonller.BaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    /**
     * 这是分页
     * @return
     */
    @RequestMapping("/list")
    public String findByPage(@RequestParam(defaultValue ="1") Integer pageNum,
                             @RequestParam(defaultValue = "5")Integer pageSize){
        //权限校验
        SecurityUtils.getSubject().checkPermission("用户管理");
        String companyId=getLoginCompanyId();
        PageInfo<User> pageInfo = userService.findByPage(companyId, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "system/user/user-list";

    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        List<Dept> list = deptService.findAll(getLoginCompanyId());
        request.setAttribute("list",list);
        return "system/user/user-add";
    }

    @RequestMapping("/edit")
    public  String edit(User user){
        user.setCompanyId(getLoginCompanyId());
        user.setCompanyName(getLoginCompanyName());
        if(StringUtils.isEmpty(user.getId())){
            userService.save(user);
        }else {
            userService.update(user);
        }
        return "redirect:/system/user/list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        User user = userService.findById(id);
        List<Dept> deptList = deptService.findAll(getLoginCompanyId());
        request.setAttribute("user",user);
        request.setAttribute("deptList",deptList);
        return "system/user/user-update";
    }

    @RequestMapping("/delete")
    @ResponseBody//放回字符串
    public Map delete(String id){
        Map<String,Integer> result=new HashMap<>();
        Boolean delete = userService.delete(id);
        if(delete){
            result.put("message",1);
        }else {
            result.put("message",0);
        }
        return result;
    }

    @RequestMapping("/roleList")
    public String roleList(String id){
        User user = userService.findById(id);
        List<Role> roleList = roleService.findAll(getLoginCompanyId());
        List<Role> userRole= roleService.findUserRoleByUserId(id);
      // 定义角色字符串，存储用户已经拥有的所有角色id，用逗号隔开
        String userRoleId="";
        if(userRole!=null){
            for (Role role : userRole) {
                userRoleId+=role.getId()+",";
            }
        }
        request.setAttribute("user",user);
        request.setAttribute("roleList",roleList);
        request.setAttribute("userRoleIds",userRoleId);
        return "system/user/user-role";
    }

    @RequestMapping("/changeRole")
    public String changeRole(String userId,String[] roleIds){
            userService.changeRole(userId,roleIds);
            return "";
    }
}
